//
//  THSuggestViewController.m
//  TreeHole
//
//  Created by apple on 2020/9/9.
//  Copyright © 2020 stoneobs. All rights reserved.
//

#import "THCommentOrderGoodViewController.h"
#import "STTools_TextView.h"
@interface THCommentOrderGoodViewController ()
@property(nonatomic, strong) STTools_TextView                     *text_view;/**<  */
@property(nonatomic, strong) STTools_ImageAutoChoseView                     *autoView;/**<  */
@end

@implementation THCommentOrderGoodViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    self.title = @"商品评价";
    [self config_tableFooter];
    [self stoneobs_fun_st_setRightItemWithTitle:@"发布" titleColor:STONEOBS_UI_FIRST_TEXT_COLOR];
    // Do any additional setup after loading the view.
}
#pragma mark --configSubView
- (void)config_tableFooter{
    UIView * footer = [[UIView alloc] initWithFrame:STONEOBS_UI_UISCREEN_FRAME];
    
    CGFloat top = 10;
    ({
        UIView * whiteView = [[UIView alloc] initWithFrame:CGRectMake(12, top, STONEOBS_UI_UISCREEN_WIDTH - 24, 156)];
        whiteView.backgroundColor = UIColor.whiteColor;
        whiteView.layer.cornerRadius = 8;
        whiteView.clipsToBounds = YES;
        [footer addSubview:whiteView];
        top = whiteView.bottom + 15;
        
        
        if (!self.text_view) {
            self.text_view = [[STTools_TextView alloc] initWithFrame:CGRectMake(10, 10, whiteView.width - 20, 260)];
            self.text_view.placeholder = @"输入您的评价";
            self.text_view.label.left = 15;
            self.text_view.label.top = 5;
            self.text_view.backgroundColor = UIColor.clearColor;
        }
        [whiteView addSubview:self.text_view];
        
        
        if (!self.autoView) {
            __weak typeof(self) weakSelf =  self;
            self.autoView = [[STTools_ImageAutoChoseView alloc] initWithFrame:CGRectMake(10,self.text_view.bottom + 10 , whiteView.width - 20, 100)];
            [self.autoView setFrameDidChangedHandle:^(STTools_ImageAutoChoseView *autochoseView) {
                [weakSelf config_tableFooter];
            }];
        }
        whiteView.height = self.autoView.bottom + 10 + 8;
        [whiteView addSubview:self.autoView];
        top = whiteView.bottom + 20;
    });
    
    footer.height = top;
    self.tableView.tableFooterView = footer;
}
- (void)stoneobs_fun_st_rightBarAction:(id)sender{
    if (self.text_view.text.length) {
    }else{
        [SVProgressHUD showInfoWithStatus:@"请输入您的建议"];
        return;
    }
    if (self.autoView.stoneobs_fun_get_allImages) {
        NSArray * imag_array = self.autoView.stoneobs_fun_get_allImages;
        [TMQNUploadManger.manger uploadMoreImage:imag_array handle:^(BOOL success, NSArray * _Nonnull array) {
            if (array.count == imag_array.count) {
                [self send_add_suggest_request_urls:[array componentsJoinedByString:@","]];
            }else{
               [SVProgressHUD showErrorWithStatus:@"上传失败请重试"];
            }
        }];
    }
}
- (void)send_add_suggest_request_urls:(NSString*)urls{
    
    NSMutableDictionary * stoneobs_net_param_dic = [NSMutableDictionary new];
    NSString * stoneobs_net_user_id = [STTools_UserManger defult].stoneobs_fun_get_loginedUser.uid.description;
    NSString * stoneobs_net_user_token = STTools_UserManger.defult.stoneobs_fun_get_loginedUser.token;
    if (stoneobs_net_user_id.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_id forKey:@"uid"];
    }
    if (stoneobs_net_user_token.length) {
        [stoneobs_net_param_dic setObject:stoneobs_net_user_token forKey:@"token"];
    }
    if (urls.length) {
        [stoneobs_net_param_dic setObject:urls forKey:@"imgs"];
    }
    if (self.text_view.text.length) {
        [stoneobs_net_param_dic setObject:self.text_view.text forKey:@"evaluate"];
    }else{
        [SVProgressHUD showInfoWithStatus:@"请输入您的建议"];
        return;
    }
    [SVProgressHUD showWithStatus:@"请稍后"];
    [stoneobs_net_param_dic setObject:@"2" forKey:@"type"];
    if (self.oid.length) {
        [stoneobs_net_param_dic setObject:self.oid forKey:@"id"];
    }
    NSString * stoneobs_net_net_url = [NSString stringWithFormat:@"%@%@",RELEASE_SERVER_URL_HEADER,@"v1/orders/evaluate"];
    DDLogInfo(@"url:%@",stoneobs_net_net_url);
    DDLogInfo(@"param:%@",stoneobs_net_param_dic);
    [[STTools_NetWrokManger defaultClient] stoneobs_fun_requestWithPath:stoneobs_net_net_url
                                                                 method:STHttpRequestType_stoneobs_enumPost
                                                             parameters:stoneobs_net_param_dic
                                                                success:^(NSURLSessionDataTask *operation, id responseObject) {
        [SVProgressHUD dismiss];
        NSDictionary * stoneobs_net_model_dic = [responseObject valueForKey:@"data"];
        [SVProgressHUD showSuccessWithStatus:@"感谢您的评价"];
        dispatch_after(dispatch_time(DISPATCH_TIME_NOW, (int64_t)(1 * NSEC_PER_SEC)), dispatch_get_main_queue(), ^{
            [self.navigationController popToRootViewControllerAnimated:YES];
        });
        // DDLogInfo(@"json = \n%@",modelDic.mj_JSONString);
        DDLogInfo(@"%@请求成功:resp\n%@",stoneobs_net_net_url,responseObject);
        
        
    } failure:^(NSString *stateCode, STTools_Error *error,NSError *originError) {
        [SVProgressHUD dismiss];
        [SVProgressHUD showErrorWithStatus:error.desc];
        DDLogError(@"\n请求失败:\nurl:%@\nparam:%@\n失败原因:%@\n错误码:%ld",
                   stoneobs_net_net_url,stoneobs_net_param_dic,error.desc,error.code);
    }];
}
@end
